﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

using MessagingToolkit.Barcode;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using ESF.General;
using MessagingToolkit.Barcode.QRCode.Decoder;

#endregion


namespace ESF.WinForms.Controls.Barcodes
{
	public class QRStickerPrintPage : PrintDocument
	{
		#region Propiedades

		public int PageCount
		{
			get 
			{
				return (int) Math.Ceiling (StickerData.Count * 1.0 / StickerPageSettings.StickerPageCount);
			}
		}

		#endregion


		#region Miembros

		public List<StickerData> StickersInfo;

		public List<Font> StickerFonts;

		private List<StickerData> StickerData;

		private BarcodeEncoder _BarcodeEncoder;

		private int _CurrentPrintIndexItem;

		public StickerPageSettings StickerPageSettings;

		public bool ShowBarcodeSeparated;
		
		public bool ShowBarcode;

		#endregion


		#region Constructor

		public QRStickerPrintPage (PrinterSettings pPrinterSettings, StickerPageSettings pStickerPageSettings, List<StickerData> pStickerData, List<Font> pStickerFonts)
		{
			DocumentName = "Sticker Barcodes";

			DefaultPageSettings = new PageSettings (pPrinterSettings);
				
			StickerData = pStickerData;

			StickerFonts = pStickerFonts;

			StickerPageSettings = pStickerPageSettings;

			_BarcodeEncoder = new BarcodeEncoder ();
			_BarcodeEncoder.MarginSize = 0;
			_BarcodeEncoder.Alignment = AlignmentPositions.Left;
			_BarcodeEncoder.ImageFormat = ImageFormat.Bmp;
			_BarcodeEncoder.IncludeLabel = false;
			_BarcodeEncoder.CharacterSet = "UTF-8";
			_BarcodeEncoder.ErrorCorrectionLevel = ErrorCorrectionLevel.H;
			_BarcodeEncoder.Width = pStickerPageSettings.BarcodeWidth;
			_BarcodeEncoder.Height = pStickerPageSettings.BarcodeHeight;
			_BarcodeEncoder.ModuleSize = pStickerPageSettings.BarcodeModuleSize;
		}


		#endregion
		

		#region Funciones de Impresion

		protected override void OnBeginPrint(PrintEventArgs e)
		{
			base.OnBeginPrint(e);

			_CurrentPrintIndexItem = 0;

			DefaultPageSettings.Margins = new Margins (
				(int) Math.Ceiling (StickerPageSettings.PageMarginLeft_in100),
				(int) Math.Ceiling (StickerPageSettings.PageMarginRight_in100),
				(int) Math.Ceiling (StickerPageSettings.PageMarginTop_in100),
				(int) Math.Ceiling (StickerPageSettings.PageMarginBottom_in100));

			DefaultPageSettings.Landscape = false;
			
			DefaultPageSettings.Color = false;

			DefaultPageSettings.PaperSize = StickerPageSettings.PaperSize;
		}


		protected override void OnPrintPage(PrintPageEventArgs e)
		{
			float xPos;
			float yPos;

			float lineYPos;
			float lineXPos;

			Image barcodeImage;


			//e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

			// comienza en el margen
			yPos = e.MarginBounds.Top;
			
			for (int rowIndex = 0; rowIndex < StickerPageSettings.StickerPageCount / StickerPageSettings.StickerRowCount; rowIndex ++)
			{
				// comienza en el margen
				xPos = e.MarginBounds.Left;

				for (int colIndex = 0; colIndex < StickerPageSettings.StickerRowCount; colIndex ++)
				{
					// imprime el borde del sticker
					if (StickerPageSettings.StickerIncludeBorders)
					{
						e.Graphics.DrawRectangle (Pens.Silver,
							xPos, 
							yPos,  
							StickerPageSettings.StickerWidth_in100, 
							StickerPageSettings.StickerHeight_in100);
					}

					// imprime el contenido del sticker
					barcodeImage = (Image) (ImageHelper.Crop ((Bitmap) _BarcodeEncoder.Encode (StickerPageSettings.BarcodeFormat, StickerData [_CurrentPrintIndexItem].BarCode)));
					
					e.Graphics.DrawImage (barcodeImage, xPos + 4, yPos + ((StickerPageSettings.StickerHeight_in100 - barcodeImage.Height) / 2));
					

					lineYPos = yPos + 4;
					lineXPos = (float) (xPos + barcodeImage.Width + 10);

					for (int i = 0; i < StickerData [_CurrentPrintIndexItem].Texts.Count; i++)
					{
						e.Graphics.DrawString (StickerData [_CurrentPrintIndexItem].Texts [i], StickerData [_CurrentPrintIndexItem].TextFonts [i], Brushes.Black, lineXPos, lineYPos);
						lineYPos += StickerData [_CurrentPrintIndexItem].TextFonts [i].GetHeight (e.Graphics);
						lineYPos += 2;
					}

					lineYPos += 10;

					if (ShowBarcode)
					{	
						e.Graphics.DrawString (StickerData [_CurrentPrintIndexItem].BarCode, StickerData [_CurrentPrintIndexItem].BarcodeTextFont, Brushes.Black, lineXPos, lineYPos);
					}

					else if (ShowBarcodeSeparated)
					{
						e.Graphics.DrawString (StickerData [_CurrentPrintIndexItem].General, StickerData [_CurrentPrintIndexItem].BarcodeTextFont, Brushes.Black, lineXPos, lineYPos);
						
						lineYPos += StickerData [_CurrentPrintIndexItem].BarcodeTextFont.GetHeight (e.Graphics);
						lineYPos += 3;

						e.Graphics.DrawString (StickerData [_CurrentPrintIndexItem].Particular, StickerData [_CurrentPrintIndexItem].BarcodeTextFont, Brushes.Black, lineXPos, lineYPos);
					}
											
					_CurrentPrintIndexItem ++;

					if (StickerData.Count == _CurrentPrintIndexItem)
					{
						e.HasMorePages = false;
						return;
					}

					// se desplaza el ancho del sticker + el espacio horizontal
					xPos += StickerPageSettings.StickerHorizontalSeparator_in100 + StickerPageSettings.StickerWidth_in100;
				}

				// Se desplaza el alto del sticker + el espacio vertical
				yPos += StickerPageSettings.StickerHeight_in100 + StickerPageSettings.StickerHorizontalSeparator_in100;
			}

			e.HasMorePages = (StickerData.Count - 1 > _CurrentPrintIndexItem);
		}

		#endregion
	}
}
